<template>
	<view class="route-recharge">
		<!-- 	<view class="inner">
			<view class="tit">
				充值套餐
			</view>
			<view class="cont">
				<view @click="handleChangeActive(item)" :class="`cloum ${blockpackageid===item.id?'active':''}`"
					v-for="(item,index) in blocksList" :key="index">
					<view class="val">
						{{item.price}}元
					</view>
					<view class="name">
						{{item.name}}
					</view>
				</view>
			</view>
		</view> -->
		<view @click="handleChangeActive(item)" v-for="(item,index) in blocksList" :key="index" class="one-package">
			<image :src="item.image" mode="aspectFill" class="package-img"></image>
			<view class="package-info">
				<view class="package-name">{{item.name}}</view>
				<view class="package-price">{{item.price}} 元</view>
			</view>
			<image v-if="blockpackageid===item.id" src="/static/images/checked.png" mode="widthFix" class="check-img">
			</image>
			<image v-else src="/static/images/check.png" mode="widthFix" class="check-img"></image>
		</view>
		<view class="footer">
			<view class="btn" @click="handleRecharge">
				充值
			</view>
			<button open-type="share" class="btn" style="margin-top: 20rpx;" v-if="type===2">
				分享
			</button>
		</view>
	</view>
</template>

<script setup>
	import subScribeMsg from '@/utils/subScribeMsg.js'
	import handleCache from "@/utils/cache.js"
	import {
		balanceApi,
		userApi
	} from '@/api/index.js'
	import {
		ref,
		computed
	} from 'vue'
	import {
		onReady,
		onShow,
		onLoad,
	} from '@dcloudio/uni-app'
	const userInfo = ref('')
	const blocksList = ref([])
	const blockpackageid = ref('')
	const type = ref(1)
	const shareShanghuId = ref('')
	onShow(async () => {
		userInfo.value = handleCache.get('userInfo')
		if (shareShanghuId.value) {
			if (!handleCache.get('userInfo')) {
				uni.navigateTo({
					url: `/pages/login/login?path=/pages/recharge/recharge?type=${type.value}&shanghuId=${shareShanghuId.value}&pid=${userInfo.value.id}`
				})
			} else {
				await userApi.userstore({
					uid: userInfo.value.id,
					shanghuid: shareShanghuId.value
				}).then((res) => {
					if (res.code == 0) {
						let data = {
							...userInfo.value
						}
						data.shanghuid = shareShanghuId.value
						userInfo.value = data
						handleCache.remove('userInfo')
						handleCache.set('userInfo', data, 'forever')
					}
				})
			}
		}
		await getData()
		if (type.value === 2) {
			uni.setNavigationBarTitle({
				title: '充值购'
			})
			uni.$mpShare = shareData.value
		}

	})
	onLoad((options) => {

		if (options.shanghuid) {
			shareShanghuId.value = Number(options.shanghuid)

		}
		if (options.type) {
			type.value = Number(options.type)

		}
		if (options.pid) {
			if (options.type == 2) {
				getApp().globalData.shareUserId = Number(options.pid)
				if (handleCache.get('userInfo')) {
					userApi.bangdingsj({
						uid: handleCache.get('userInfo').id,
						pid: Number(options.pid)
					})
				}
			}

		}

	})
	const shareData = computed(() => { // 分享的数据
		return {
			title: `${uni.getStorageSync('userInfo').value.shanghuname||''}-充值购`,
			path: `/pages/recharge/recharge?type=${type.value}id=${userInfo.value.id}&shanghuid=${userInfo.value.shanghuid}`,
			desc: '快来看看',
		}
	})
	const getData = () => {
		balanceApi.blocks({
			shanghuid: userInfo.value.shanghuid,
			state: type.value
		}).then((res) => {
			blocksList.value = res.data
			if (blocksList.value.length) {
				blockpackageid.value = blocksList.value[0].id
			}
		})
	}
	const handleChangeActive = (data) => {
		blockpackageid.value = data.id
	}
	const handleRecharge = async () => {
		if (!blockpackageid.value) {
			return
		}
		if (type.value === 2) {
			await subScribeMsg(['Kj8bNW0GPT0RnaKCVKKT0_ryLsog6V2jrB2T-AwGWJw'])
		}
		let req = {
			uid: userInfo.value.id,
			blockpackageid: blockpackageid.value
		}
		uni.showLoading({
			text: '加载中'
		})
		balanceApi.wechatpay(req).then((res) => {
			uni.hideLoading()
			if (res.code === 0) {
				let paydata = res.data

				wx.requestPayment({
					nonceStr: paydata.nonceStr, // 随机字符串
					package: paydata.package, // 固定值
					signType: paydata.signType, // 统一下单订单号 
					timeStamp: paydata.timeStamp, // 时间戳（单位：秒）
					paySign: paydata.sign, // 签名，这里用的 MD5/RSA 签名
					success(res) {
						if (type.value === 2) {
							uni.navigateTo({
								url: "/pagesA/rechargeorder/rechargeorder"
							})
							return
						}
						uni.navigateTo({
							url: '/pages/payresult/payresult?status=1'
						})
					},
					fail(e) {
						uni.navigateTo({
							url: '/pages/payresult/payresult?status=0'
						})

					}
				})
			} else {
				uni.showToast({
					mask: true,
					title: res.msg,
					icon: 'none'
				})
			}
		})
	}
</script>

<style lang="scss" scoped>
	button {
		margin: 0;
		padding: 0;
		background-color: transparent;
		line-height: 32rpx;
	}

	button::after {
		border: none;
	}

	.route-recharge {
		padding: 30rpx 24rpx;

		.inner {
			background-color: #fff;
			border-radius: 10rpx;
			padding: 24rpx;

			.tit {
				font-weight: 500;
				font-size: 30rpx;
			}

			.cont {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;

				.cloum {
					margin-top: 20rpx;
					width: calc(50% - 10rpx);
					height: 100rpx;
					border: 1px solid #edf0f2;
					border-radius: 12rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					background-color: #fff;
				}

				.val {
					font-weight: 500;

				}

				.name {
					font-size: 24rpx;
					margin-top: 5rpx;
					color: #999;
				}

				.active {
					border-color: #EE382F;

					color: #EE382F;
				}
			}

		}

		.footer {
			margin-top: 40rpx;

			.btn {
				width: 100%;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: #EE382F;
				color: #fff;
				border-radius: 50rpx;
			}
		}
	}

	.one-package {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		width: 700rpx;
		background: #fff;
		border-radius: 20rpx;
		padding: 20rpx 20rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
		margin-top: 20rpx;
		position: relative;
	}

	.package-img {
		width: 200rpx;
		height: 200rpx;
		border-radius: 10rpx;
		margin-right: 20rpx;
	}

	.package-info {
		width: calc(100% - 220rpx);
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-start;
	}

	.package-name {
		font-size: 28rpx;
		width: 100%;
	}

	.package-price {
		color: #EE382F;
		font-size: 36rpx;
	}

	.check-img {
		position: absolute;
		right: 20rpx;
		width: 40rpx;
		height: 40rpx;
		top: 100rpx;
	}
</style>